package gui.data.D3;

import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.PolyhedronCalculator;
import calcs.XYZDistanceCalculator;

public class PolyhedronInputGui extends JPanel implements ActionListener{
	JTextField radiusInput, areaInput, bAreaInput, sAreaInput, heightInput, volumeInput, sideInput;
	MyFrame myFrame;
	JLabel radiusLbl, areaLbl, bAreaLbl, sAreaLbl, heightLbl, volumeLbl, sideLbl;
	int guide = 1;

	public PolyhedronInputGui(final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(0, 2));
		this.setPreferredSize(new Dimension(500, 500));
		
		ButtonGroup group = new ButtonGroup();
		
		JRadioButton cylinder = new JRadioButton("Cylinder");
		cylinder.setActionCommand("cylinder");
		cylinder.addActionListener(this);
		
		JRadioButton prism = new JRadioButton("Prism");
		prism.setActionCommand("prism");
		prism.addActionListener(this);
		
		group.add(cylinder);
		group.add(prism);
		
		radiusLbl = new JLabel("Cylinder radius = ");
		areaLbl = new JLabel("Total area = ");
		sideLbl = new JLabel("Bottom edge length = ");
		sAreaLbl = new JLabel("Side area = ");
		bAreaLbl = new JLabel("Bottom area = ");
		volumeLbl = new JLabel("Volume = ");
		heightLbl = new JLabel("Height = ");
		
		radiusInput = new JTextField();
		areaInput = new JTextField();
		sideInput = new JTextField();
		sAreaInput = new JTextField();
		bAreaInput = new JTextField();
		volumeInput = new JTextField();
		heightInput = new JTextField();
		
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		
		this.add(cylinder);
		this.add(prism);		
		this.add(radiusLbl);
		this.add(radiusInput);
		this.add(sideLbl);
		this.add(sideInput);
		this.add(heightLbl);
		this.add(heightInput);
		this.add(areaLbl);
		this.add(areaInput);
		this.add(sAreaLbl);
		this.add(sAreaInput);
		this.add(bAreaLbl);
		this.add(bAreaInput);
		this.add(volumeLbl);
		this.add(volumeInput);
		this.add(new JLabel(""));
		this.add(calculate);
		
		cylinder.setSelected(true);
		sideInput.setEnabled(false);
	}
	private void openResult() {
		
		boolean error = false;
		JTextField[] textFields = {radiusInput, sideInput, volumeInput, areaInput, sAreaInput, bAreaInput, heightInput};
		Double[] values = new Double[7];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new PolyhedronCalculator(guide, values[0], values[1], values[2], values[3], values[4], values[5], values[6], myFrame);
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("cylinder")){
			guide = 1;
			sideInput.setEnabled(false);
			radiusInput.setEnabled(true);
			sideInput.setText("");
		}else if(e.getActionCommand().equals("prism")){
			guide = 2;
			sideInput.setEnabled(true);
			radiusInput.setEnabled(false);
			radiusInput.setText("");
		}
	}
}
